package com.aiti.lulian.controller.crad;

import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.annotates.EditGroup;
import com.aiti.lulian.annotates.QueryGroup;
import com.aiti.lulian.bo.CustomerFollowUpRecordBo;
import com.aiti.lulian.entity.CustomerFollowUpRecord;
import com.aiti.lulian.service.ICustomerFollowUpRecordService;
import com.aiti.lulian.vo.CustomerFollowUpRecordVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 客户询盘跟进记录
 * @since 2024-11-12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/customer_follow_up_record")
public class CustomerFollowUpRecordController extends WebController<CustomerFollowUpRecord> {

    private final ICustomerFollowUpRecordService baseService;

    /**
     * 查询客户的跟进记录
     * @param bo 前端传入的参数
     * @return 跟进的记录
     */
    @GetMapping("/list")
    public Message list(@Validated(QueryGroup.class) CustomerFollowUpRecordBo bo) {
        Integer pageSize = bo.getPageSize();
        Integer pageNum = bo.getPageNum();
        Page<CustomerFollowUpRecord> page = startPage(pageNum,pageSize);
        Map<String, Object> result = baseService.queryCustomerList(page, bo);
        result.put("total", page.getTotal());
        return Message.success(result);
    }

    /**
     * 根据跟进人查询客户跟进记录
     */
    @ApiOperation("根据跟进人查询客户跟进记录")
    @GetMapping("/getList")
    public Message getCustomerFollowUpRecordList(@Validated(QueryGroup.class) CustomerFollowUpRecordBo bo) {
        Integer pageSize = bo.getPageSize();
        Integer pageNum = bo.getPageNum();
        Page<CustomerFollowUpRecord> page = startPage(pageNum, pageSize);
        List<CustomerFollowUpRecordVo> customerFollowUpRecords = baseService.getCustomerFollowUpRecordList(page, bo);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", customerFollowUpRecords);
        resultMap.put("total", page.getTotal());
        return Message.success(resultMap);
    }

    /**
     * 新增跟进记录
     * @param bo 参数
     * @return 结果
     */
    @PostMapping("/add")
    public Message addFollowUpRecord(@RequestBody CustomerFollowUpRecordBo bo) {
        boolean b = baseService.addFollowUpRecord(bo);
        return Message.success(b);
    }

    /**
     * 查询某个记录的详情
     * @param bo 参数
     * @return 结果
     */
    @GetMapping("/queryCustomerFollowUpRecordDetail")
    public Message queryCustomerFollowUpRecordDetail(@Validated(QueryGroup.class) CustomerFollowUpRecordBo bo) {
        CustomerFollowUpRecordVo result = baseService.queryCustomerFollowUpRecordDetail(bo);
        return Message.success(result);
    }

    /**
     * 更新根进记录
     * @param bo 参数
     * @return 结果
     */
    @PostMapping("/update")
    public Message editCustomerFollowUpRecord(@Validated(EditGroup.class)@RequestBody CustomerFollowUpRecordBo bo) {
        return Message.success(baseService.editCustomerFollowUpRecord(bo));
    }

    @PostMapping("/delete")
    public Message deleteShangJiFollowUpRecord(@RequestBody CustomerFollowUpRecordBo bo) {
        if(StringUtils.isBlank(bo.getBaseId())){
            return Message.fail("没有找到主键");
        }
        return Message.success(baseService.deleteShangJiFollowUpRecord(bo));
    }

    @ApiOperation("删除跟进记录（支持批量）")
    @DeleteMapping("/delete")
    public Message deleteCustomerFollowUpRecord(@RequestParam("baseIds") String[] baseIds) {
        return baseService.deleteCustomerFollowUpRecord(baseIds);
    }
}